<!--
 * @Author: zhangduo 445629906@qq.com
 * @Date: 2023-12-10 22:20:43
 * @LastEditors: zhangduo 445629906@qq.com
 * @LastEditTime: 2023-12-25 03:59:31
 * @FilePath: \mdbj-ui\src\components\Map\road\roadInceptionDataPanel.vue
 * @Description: 
 * 
 * Copyright (c) 2023 by zhangduo, All Rights Reserved. 
-->
<template>
  <div>
    <el-table
      :data="features"
      v-loading="loading"
      element-loading-text="正在加载中"
      stripes
      highlight-current-row
      :height="tableHeight"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column
        label="道路编码"
        align="center"
        prop="attributes.roadcode"
        width="100"
      ></el-table-column> -->
      <el-table-column
        label="巡查人员"
        width="100"
        align="center"
        :show-overflow-tooltip="true"
        prop="attributes.inspector"
      ></el-table-column>
      <el-table-column
        label="巡查时间"
        align="center"
        :show-overflow-tooltip="true"
        prop="attributes.inspectiontime"
        :formatter="dateFormat"
      ></el-table-column>
      <el-table-column
        label="巡查结果"
        align="center"
        :show-overflow-tooltip="true"
        prop="attributes.inspectionresult"
      ></el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleViewInception(scope.row)"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      layout="total, sizes, prev, pager, next"
      @pagination="getList"
    />
  </div>
</template>

<script>
import FeatureLayer from "@arcgis/core/layers/FeatureLayer";

export default {
  props: {
    mapview: { default: () => {} },
    paramObj: { default: () => {} },
    roadcode: {
      type: String,
    },
    isedit: {
      type: Boolean,
    },
  },
  inject: ["toggleComponent"],
  data() {
    return {
      tableHeight: "35vh",
      loading: true,
      // 查询参数
      queryParams: {
        startDate: "",
        endDate: "",
        pageNum: 1,
        pageSize: 20,
        roadcode: "12",
        roadname: undefined,
      },
      // 是否显示查询面板
      showSearch: true,
      // 当前页要素集合
      features: [],
      //总条数
      total: 0,
      // 要素图层
      featureLayer: undefined,
      // 要素图层url
      featureLayerUrl:
        window.mapConfig.operationLayers.featurelayers["inspectionrecords"].url,
      // 查询字符串
      whereClause: undefined,
      // 选中数据行
      selectFeatures: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      featureLayer: null,
      feature: undefined,
    };
  },
  mounted() {
    console.log("mounted");
    let _self = this;
    _self.getList();
  },
  activated() {
    console.log("activated");
    let _self = this;
    _self.getList();
    // this.initFeatureLayer();
  },
  deactivated() {
    let _self = this;
    // _self.roadcode = undefined;
    // _self.roadname = undefined;
  },
  methods: {
    /*日期处理*/
    dateFormat(row, column, cellValue, index) {
      // let date = row[column.property];
      const date = new Date(cellValue); //先把时间戳格式转化为日期格式
      // let localTime = this.$moment(date).format("YYYY-MM-DD HH:mm:ss");
      let localTime = this.$moment(date).format("YYYY-MM-DD");
      return localTime;
    },

    /**
     * @description: 切换查询面板显示隐藏
     * @param {*} val
     * @return {*}
     */
    switchChange(val) {
      console.log(val);
      if (val) {
        this.tableHeight = "70vh";
      } else this.tableHeight = "75vh";
    },
    /**
     * @description: 调用arcgis js api查询数据
     * @return {*}
     */
    getList() {
      let _self = this;
      _self.loading = true;
      _self.featureLayer = new FeatureLayer({
        title: "巡查记录管理",
        url: _self.featureLayerUrl,
        outFields: ["objectid"],
      });
      _self.queryParams.roadcode = _self.roadcode;
      _self.whereClause = _self.getWhereClause();
      // 先构建用于查询数据总条数的查询参数对象
      let queryParams = {
        // 输出字段
        outFields: ["objectid"],
        where: _self.whereClause,
      };
      // 首先查询数据总条数用于渲染分页组件值
      _self.featureLayer.queryFeatureCount(queryParams).then((numResults) => {
        _self.total = numResults;
      });
      // 分页查询并在表格展示数据
      queryParams = {
        start: (_self.queryParams.pageNum - 1) * _self.queryParams.pageSize,
        num: _self.queryParams.pageSize,
        outFields: ["*"],
        orderByFields: ["inspectiontime desc"],
        where: _self.whereClause,
      };
      // 查询数据
      _self.featureLayer.queryFeatures(queryParams).then((featureSet) => {
        _self.features = featureSet.features;
        _self.loading = false;
      });
    },
    /**
     * @description: 根据查询参数构建where查询条件
     * @return {*}
     */
    getWhereClause() {
      let _self = this;
      let whereClause = "1=1";
      if (_self.queryParams.startDate && _self.queryParams.endDate)
        whereClause += ` and inspectiontime between '${_self.queryParams.startDate}' and '${_self.queryParams.endDate}'`;
      if (_self.queryParams.roadcode)
        whereClause += ` and roadcode = '${_self.queryParams.roadcode}'`;
      return whereClause;
    },

    /**
     * @description: 查询按钮
     * @return {*}
     */
    handleQuery() {
      let _self = this;
      _self.queryParams.pageNum = 1;
      _self.getList();
    },
    /**
     * @description: 重置按钮
     * @return {*}
     */
    resetQuery() {
      let _self = this;
      _self.queryParams.startDate = undefined;
      _self.queryParams.endDate = undefined;
      _self.queryParams.pageNum = 1;
      _self.queryParams.pageSize = 10;
      _self.whereClause = undefined;
      _self.clearGraphics();
      _self.getList();
    },
    /**
     * @description: 清除临时图形
     * @return {*}
     */
    clearGraphics() {
      let _self = this;
      _self.mapview.popup.close();
      _self.mapview.graphics.removeAll();
      if (_self.highlight != undefined) {
        _self.highlight.remove();
      }
    },
    /**
     * @description: 详情按钮
     * @param {*} row
     * @return {*}
     */
    async handleViewInception(row) {
      let _self = this;
      // let queryParams = {
      //   outFields: ["*"],
      //   where: `objectid=${row.attributes["objectid"]}`,
      //   returnGeometry: true,
      // };
      // const featureSet = await _self.featureLayer.queryFeatures(queryParams);
      // const feature = featureSet.features[0];
      let paramObj = {
        feature: row,
        featureLayer: _self.featureLayer,
      };
      _self.toggleComponent("roadInceptionDetailPanel", paramObj);
    },
    async handleUpdateInception(row) {
      let _self = this;
      let queryParams = {
        outFields: ["*"],
        where: `objectid=${row.attributes["objectid"]}`,
        // returnGeometry: true,
      };
      const featureSet = await _self.featureLayer.queryFeatures(queryParams);
      const feature = featureSet.features[0];
      let paramObj = {
        feature: feature,
        featureLayer: _self.featureLayer,
      };
      _self.toggleComponent("inceptionEditPanel", paramObj);
    },
    handleDelete(row) {
      let _self = this;
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          const deleteFeatures = [row];
          const edits = {
            deleteFeatures: deleteFeatures,
          };
          // 删除要素
          _self.featureLayer.applyEdits(edits).then(() => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            _self.getList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    handleAdd() {
      let _self = this;
      let paramObj = {
        roadcode: _self.roadcode,
        roadname: _self.roadname,
        featureLayer: _self.featureLayer,
      };
      _self.toggleComponent("inceptionAddPanel", paramObj);
    },
    /**
     * @description: 多选框选中数据
     * @param {*} selection 选中的行
     * @return {*}
     */
    handleSelectionChange(selection) {
      // this.ids = selection.map((item) => item.attributes.objectid);
      this.selectFeatures = selection;
      this.multiple = !selection.length;
    },
    // 返回上一页面
    goBack() {
      this.toggleComponent("dataPanel");
    },
  },
};
</script>

<style lang="scss" scoped>
.title-row {
  width: 100%;
  font-size: 18px;
  font-weight: bold;
  color: #409eff;
  margin-bottom: 1vh;
}
</style>
